#include <iostream>
#include <vector>
#include <stdlib.h>//srand(),rand()
#include <sys/time.h>

using namespace std;

const int N = 2000000;

int main ()
{
	timeval starttime,endtime;
	long int timeuse;
	vector<int> buf(N, 0);
	vector<int>::iterator start=buf.begin();
	vector<int>::iterator end=buf.end();
	vector<int>* pStart = &buf;
	vector<int>* pEnd = pStart + N;
	int i = 0;

	vector<int>::iterator pit=buf.begin();
	gettimeofday(&starttime,0);
	//计数器实现循环
	while ( i < N )
	{
		*pit = i++;
		pit++;
	}
	gettimeofday(&endtime,0);
	timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec;
	cout << timeuse << " us" << endl;

	
	gettimeofday(&starttime,0);
	//迭代器实现循环，条件判断中无函数调用
	for ( vector<int>::iterator it = start; it != end; it++ )
	{
		*it = i--;
	}
	gettimeofday(&endtime,0);
	timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec;
	cout << timeuse << " us" << endl;
	
	
	gettimeofday(&starttime,0);
	//迭代器实现循环，条件判断中有函数调用
	for ( vector<int>::iterator it = buf.begin(); it != buf.end(); it++ )
	{
		*it = i++;
	}
	gettimeofday(&endtime,0);
	timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec;
	cout << timeuse << " us" << endl;
	
	return 0;
}
